<?php

/**
 * класс для работы с таблицей файлов, прикрепленных к заявкам
 *
 * @author kingop
 */
class DatabaseObject_ProductsImg extends DatabaseObject {

    public function __construct($db) {
        parent::__construct($db, 'products_img', 'id');

        $this->add('productId');
        $this->add('filename');
        $this->add('image');
        $this->add('thumb');
    }
    
    protected function postDelete() {
        parent::postDelete();
         if (file_exists(self::GetUploadDirectory() .'/'. $file->filename))
                unlink(self::GetUploadDirectory() .'/'. $file->filename);
         return true;
    }

    public static function DeleteProductsImgID($db, $id) {
        $file = new DatabaseObject_ProductsImg($db);
        $file->load($id);
        if (file_exists(self::GetUploadDirectory() .'/'. $file->filename))
            unlink(self::GetUploadDirectory() .'/'. $file->filename);
        $file->delete();
        return true;
    }

    public static function DeleteProductsImgFilename($db, $id) {
        $file = new DatabaseObject_ProductsImg($db);
        $file->load($id, 'filename');
        if (file_exists(self::GetUploadDirectory() .'/'. $file->filename))
            unlink(self::GetUploadDirectory() .'/'. $file->filename);
        $file->delete();
        return true;
    }

    public static function DeleteProductsImg($db, $id,$flag=true) {
        $files = DatabaseObject_ProductsImg::GetProductsImg($db, $id);
         
        foreach ($files as $val) {
            if ($val->getId())
            {
              if (file_exists(self::GetUploadDirectory() .'/'. $val->filename) && $flag==true)
                 unlink(self::GetUploadDirectory() .'/'. $val->filename);
            }
        }
        $query = sprintf("delete from %s where productId = %d", ' products_img', $id);

        $db->query($query);
        return true;
    }

    public static function UploadFile($db, $productId, $file) {


        $tFile = new DatabaseObject_ProductsImg($db);
        $tFile->image = $file;
        $tFile->filename = $file;
        $tFile->thumb = 'tmp';
        $tFile->productId = $productId;
        $tFile->save();
        if (!file_exists(self::GetUploadDirectory() .'/'. $file)){
        $temp = self::GetUploadDirectory() . '/' . $file;
        $tFile->image = '/uploads/' . $file;
        $tFile->thumb = '/uploads/' . $file;
        $tFile->save();
        }
        // if (rename($temp, self::GetUploadDirectory() . '' . $tFile->image)) {
        return true;
        // }
        // return false;
    }

    public static function GetUploadDirectory() {
        return Zend_Registry::get('config')->paths->base . "/www/uploads";
    }
    public static function GetUploadLogoDirectory() {
        return Zend_Registry::get('config')->paths->base . "/www";
    }

    public static function GetUrlDirectory() {
        return 'http://' . Zend_Registry::get('config')->domain . "";
    }

    private static function _GetBaseQuery($db, $options) {
        // инициализация настроек
        $defaults = array();

        foreach ($defaults as $k => $v) {
            $options[$k] = array_key_exists($k, $options) ? $options[$k] : $v;
        }

        // создание запроса на получение
        $select = $db->select();
        $select->from(array('pi' => 'products_img'), array());

        return $select;
    }

    public static function GetProductsImg($db, $productId) {
        $select = self::_GetBaseQuery($db, array());
        $select->from(null, '*');
        $select->where('productId = ?', $productId);

        $data = $db->fetchAll($select);

        $rFiles = self::BuildMultiple($db, __CLASS__, $data);

        return $rFiles;
    }

    public static function GetProductsImgId($db, $productId) {
        $query = ("SELECT * FROM `products_img` WHERE `productId`='$productId'");
        $data = $db->fetchAll($query);
        $rFiles = self::BuildMultiple($db, __CLASS__, $data);
        //$rFiles = new DatabaseObject_ProductsImg($db);
        //$rFiles->load($productId, 'productId');

        return $rFiles;
    }

}

?>
